package com.zhongying.jf.util.sms253;

import com.alibaba.fastjson.JSON;
import com.zhongying.jf.util.sms253.config.SmsConfig;
import com.zhongying.jf.util.sms253.model.SmsSendRequest;
import com.zhongying.jf.util.sms253.model.SmsSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;

/**
 * 普通短信发送
 */
public class SmsSendUtil {
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(SmsSendUtil.class);

    public static void main(String[] args) {
        String content = "您的验证码是:123098";
        String phone = "17621276053";
        boolean report = false;
        SmsSendResponse res = sendNormalSms(report, content, phone);
        System.out.println("content=" + res);
    }

    /**
     * 发送短信
     *
     * @param content 短信内容
     * @param phones  手机号
     * @param report  是否需要状态报告
     */
    public static SmsSendResponse sendNormalSms(boolean report, String content, String... phones) {
        SmsSendRequest smsSendRequest = new SmsSendRequest();
        //短信内容。长度不能超过536个字符，必填
        smsSendRequest.setMsg(SmsConfig.sms_sign+content.trim());
        //手机号码。多个手机号码使用英文逗号分隔，必填
        StringBuffer phoneSb = new StringBuffer(" ");
        for (String phone : phones) {
            phoneSb.append(phone);
            phoneSb.append(",");
        }
        phoneSb.deleteCharAt(phoneSb.length() - 1);
        System.out.println("phoneSb=" + phoneSb.toString());
        smsSendRequest.setPhone(phoneSb.toString().trim());
        SmsSendResponse smsSingleResponse = sendRequest(smsSendRequest);
        return smsSingleResponse;
    }

    //发送请求
    private static SmsSendResponse sendRequest(SmsSendRequest smsSendRequest) {
        SmsSendResponse smsSingleResponse = null;
        try {
            smsSendRequest.setAccount(SmsConfig.sms_account);
            smsSendRequest.setPassword(SmsConfig.sms_pwd);
            smsSendRequest.setUid(getSmsUid());
            String requestJson = JSON.toJSONString(smsSendRequest);
            logger.info("send sms request param: " + requestJson);
            String response = SmsHttpClient.sendSmsByPost(SmsConfig.smsSingleRequestServerUrl, requestJson);
            logger.info("send sms reponse param: " + response);
            smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
            logger.info("send sms reponse toString is: " + response);
        } catch (Exception ex) {
            ex.printStackTrace();
            smsSingleResponse = new SmsSendResponse();
            smsSingleResponse.setCode("104");
            smsSingleResponse.setErrorMsg("短信发送失败:" + ex.getMessage());
            smsSingleResponse.setMsgId(smsSendRequest.getUid());
            logger.info("phones[" + smsSendRequest.getPhone() + "],sms send error:" + smsSingleResponse.getErrorMsg());
        }
        return smsSingleResponse;
    }

    //获取短信UID
    private static String getSmsUid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

}
